package com.lc.cehui.ui.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.StoreInfoApi;
import com.lc.cehui.http.response.BusinessIndex;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.ClearEditText;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BusinessIndexActivity extends AppActivity {
    @BindView(R.id.et_search)
    ClearEditText etSearch;
    @BindView(R.id.ll_seach)
    LinearLayout llSeach;
    @BindView(R.id.rv_coupon)
    RecyclerView rvCoupon;
    @BindView(R.id.rv_business)
    RecyclerView rvBusiness;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.img_logo)
    ShapeableImageView imgLogo;
    private String store_id;
    private List<BusinessIndex.DataBean.CouponBean> dataList = new ArrayList<>();
    private List<BusinessIndex.DataBean.GoodsBean> goodsList = new ArrayList<>();
    private BaseQuickAdapter mAdapter;
    private BaseQuickAdapter goodsAdpter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_businessindex;
    }

    @Override
    protected void initView() {
        store_id = getIntent().getStringExtra("store_id");
        rvCoupon.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseQuickAdapter<BusinessIndex.DataBean.CouponBean, BaseViewHolder>(R.layout.item_coupon, dataList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, BusinessIndex.DataBean.CouponBean bean) {
                baseViewHolder.setText(R.id.tv_money, bean.getRed_price() + "");
                baseViewHolder.setText(R.id.tv_manjian, "满" + bean.getShold_price() + "可用");
                baseViewHolder.setText(R.id.tv_type, bean.getName());

                baseViewHolder.setText(R.id.tv_youxiaodate, "有效期至" + bean.getEnd_time());


            }
        };
        rvCoupon.setAdapter(mAdapter);
        goodsAdpter = new BaseQuickAdapter<BusinessIndex.DataBean.GoodsBean, BaseViewHolder>(R.layout.item_business_shop, goodsList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, BusinessIndex.DataBean.GoodsBean bean) {
                baseViewHolder.setText(R.id.title_name, bean.getName());
                GlideApp.with(BusinessIndexActivity.this).load(bean.getCover_img()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.goods_img));


                baseViewHolder.setText(R.id.tv_preprice, bean.getPrice());

            }
        };
        rvBusiness.setAdapter(goodsAdpter);
    }

    @Override
    protected void initData() {
        store_info();
    }

    private void store_info() {
        EasyHttp.post(this)
                .api(new StoreInfoApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setStore_id(store_id).setPage(1).setKey(""))
                .request(new HttpCallback<BusinessIndex>(this) {
                    @Override
                    public void onSucceed(BusinessIndex data) {
                        dataList.clear();
                        dataList.addAll(data.getData().getCoupon());
                        tvCompany.setText(data.getData().getStore().getName());
                        GlideApp.with(BusinessIndexActivity.this).load(data.getData().getStore().getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) imgLogo);
                        mAdapter.setNewData(dataList);
                        mAdapter.notifyDataSetChanged();
                        goodsList.addAll(data.getData().getGoods());
                        goodsAdpter.setNewData(goodsList);
                        goodsAdpter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
